package frame;

import java.awt.Color;
import java.awt.Font;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.net.Socket;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.SwingConstants;
import javax.swing.border.EmptyBorder;

import service.ClienteService;
import bean.Message;
import bean.Message.Action;
import javax.swing.JTextField;

public class TelaJogo extends JFrame {
	private Socket socket;
	private Message message;
	private ClienteService service;
	private boolean turn;
	private boolean creator;
	private String myNumber;
	private JLabel lblponto1;
	private JLabel lblponto2;
	private JLabel lblponto3;
	private JLabel lblponto4;
	private JLabel lblponto5;
	private JLabel lblponto6;
	private int valor;
	private boolean rodou;

	public boolean isRodou() {
		return rodou;
	}

	public void setRodou(boolean rodou) {
		this.rodou = rodou;
	}

	public int getValor() {
		return valor;
	}

	public void setValor(int valor) {
		this.valor = valor;
	}

	public String getMyNumber() {
		return myNumber;
	}

	public void setMyNumber(String myNumber) {
		this.myNumber = myNumber;
	}

	public boolean getCreator() {
		return creator;
	}

	public void setCreator(boolean creator) {
		this.creator = creator;
	}

	public boolean isTurn() {
		return turn;
	}

	public void setTurn(boolean turn) {
		this.turn = turn;
	}

	private String playerName;
	private String partida;

	public String getPartida() {
		return partida;
	}

	public void setPartida(String partida) {
		this.partida = partida;
	}

	private JLabel lblPlacar1;
	private JLabel lblJogador1;
	private JLabel lblBancada1;
	private JLabel lblPlacar2;
	private JLabel lblJogador2;
	private JLabel lblBancada2;
	private JLabel lblPlacar3;
	private JLabel lblJogador3;
	private JLabel lblBancada3;
	private JLabel lblPlacar4;
	private JLabel lblJogador4;
	private JLabel lblBancada4;
	private JButton btnA;
	private JButton btnB;
	private JButton btnC;
	private JButton btnD;
	private JButton btnE;
	private JButton btnF;
	private JButton btnG;
	private JButton btnH;
	private JButton btnI;
	private JButton btnJ;
	private JButton btnK;
	private JButton btnM;
	private JButton btnL;
	private JButton btnN;
	private JButton btnO;
	private JButton btnP;
	private JButton btnQ;
	private JButton btnR;
	private JButton btnS;
	private JButton btnT;
	private JButton btnU;
	private JButton btnV;
	private JButton btnW;
	private JButton btnX;
	private JButton btnY;
	private JButton btnZ;
	private JButton btnRodar;
	private JPanel contentPane;
	private String player1;
	private String player2;
	private String player3;
	private String player4;
	private String matchCreator;
	private String palavraRodada;
	private String mascara;
	private JLabel lblPalavra;
	private JLabel lblNewLabel;
	private String currentPlayer;
	private String nPlayers;
	private JButton bntIniciar;
	private JLabel lblNewLabel_1;
	private JTextField txtPalavra;

	public String getnPlayers() {
		return nPlayers;
	}

	public void setnPlayers(String nPlayers) {
		this.nPlayers = nPlayers;
	}

	public String getCurrentPlayer() {
		return currentPlayer;
	}

	public void setCurrentPlayer(String currentPlayer) {
		this.currentPlayer = currentPlayer;
	}

	public String getPalavraRodada() {
		return palavraRodada;
	}

	public void setPalavraRodada(String palavraRodada) {
		this.palavraRodada = palavraRodada;
	}

	public String getMascara() {
		return mascara;
	}

	public void setMascara(String mascara) {
		this.mascara = mascara;
	}

	private void askWord() {

		Message message = new Message();
		message.setName(getPlayerName());
		message.setPartida(getPartida());
		message.setAction(Action.NEW_WORD);
		this.service.send(message);
	}
	
	private void efeitoRodar(){
		int time = 100;
		for (int i = 0; i < 5; i++) {
			lblponto1.setForeground(Color.YELLOW);
			try {
				new Thread().sleep(time);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			lblponto6.setForeground(Color.YELLOW);
			lblponto1.setForeground(Color.BLACK);
			try {
				new Thread().sleep(time);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			lblponto5.setForeground(Color.YELLOW);
			lblponto6.setForeground(Color.BLACK);
			try {
				new Thread().sleep(time);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			lblponto3.setForeground(Color.YELLOW);
			lblponto5.setForeground(Color.BLACK);
			try {
				new Thread().sleep(time);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			lblponto2.setForeground(Color.YELLOW);
			lblponto3.setForeground(Color.BLACK);
			try {
				new Thread().sleep(time);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			lblponto4.setForeground(Color.YELLOW);
			lblponto2.setForeground(Color.BLACK);
			try {
				new Thread().sleep(time);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			lblponto4.setForeground(Color.BLACK);
			try {
				new Thread().sleep(time);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
	}

	public void receiveRoda(Message message){
		
		efeitoRodar();
		if(message.getRodada() == 0){
			lblponto1.setForeground(Color.YELLOW);
			setValor(100);
		}else if(message.getRodada() == 1){
			lblponto2.setForeground(Color.YELLOW);
			setValor(50);
		}else if(message.getRodada() == 2){
			lblponto3.setForeground(Color.YELLOW);
			setValor(200);
		}else if(message.getRodada() == 3){
			lblponto4.setForeground(Color.YELLOW);

				JOptionPane.showMessageDialog(this, "Passou a vez!");
				if(isTurn()){
				nextPlayer();	
			}

		}else if(message.getRodada() == 4){
			lblponto5.setForeground(Color.YELLOW);


				
				if(getCurrentPlayer().equals("1")){
					lblPlacar1.setText("0");
				}else if(getCurrentPlayer().equals("2")){
					lblPlacar2.setText("0");
				}else if(getCurrentPlayer().equals("3")){
					lblPlacar3.setText("0");
				}else if(getCurrentPlayer().equals("4")){
					lblPlacar4.setText("0");
				}
				if(isTurn()){	
				JOptionPane.showMessageDialog(this, "Perdeu tudo e passou a vez!");	
				nextPlayer();
			}


		}else if(message.getRodada() == 5){
			lblponto6.setForeground(Color.YELLOW);
			setValor(150);
		}
		btnRodar.setEnabled(false);
	}
	
	private void roda(){
		setRodou(true);
		Message message = new Message();
		message.setName(getPlayerName());
		message.setPartida(getPartida());
		message.setAction(Action.RODA);
		this.service.send(message);
		
	}
	
	public void nextPlayer() {
		Message message = new Message();
		message.setName(getPlayerName());
		message.setPartida(getPartida());
		message.setCurrentPlayer(getCurrentPlayer());
		message.setAction(Action.NEXT_PLAYER);
		setRodou(false);
		if (this.nPlayers.equals("1")) {
			message.setNplayers("1");
		} else if (this.nPlayers.equals("2")) {
			message.setNplayers("2");
		} else if (this.nPlayers.equals("3")) {
			message.setNplayers("3");
		} else if (this.nPlayers.equals("4")) {
			message.setNplayers("4");
		}

		this.service.send(message);

	}

	public void receiveWord(Message message) {
		System.out.println("RECEBEUUUU");
		System.out.println(message.getPalavra());
		System.out.println(message.getDica());

		setPalavraRodada(message.getPalavra());
		StringBuilder stringBuilder = new StringBuilder("");

		for (int i = 0; i < message.getPalavra().length(); i++) {
			stringBuilder.insert(i, "*");
		}
		setMascara(stringBuilder.toString());
		lblPalavra.setText(stringBuilder.toString());
		lblNewLabel.setText("Dica: " + message.getDica());

	}

	public String getMatchCreator() {
		return matchCreator;
	}

	public void setMatchCreator(String matchCreator) {
		this.matchCreator = matchCreator;
	}

	/**
	 * Launch the application.
	 */
	// public static void main(String[] args) {
	// EventQueue.invokeLater(new Runnable() {
	// public void run() {
	// try {
	// TelaJogo frame = new TelaJogo();
	// frame.setVisible(true);
	// } catch (Exception e) {
	// e.printStackTrace();
	// }
	// }
	// });
	// }

	public String getPlayer1() {
		return player1;
	}

	public void setPlayer1(String player1) {
		this.player1 = player1;
	}

	public String getPlayer2() {
		return player2;
	}

	public void setPlayer2(String player2) {
		this.player2 = player2;
	}

	public String getPlayer3() {
		return player3;
	}

	public void setPlayer3(String player3) {
		this.player3 = player3;
	}

	public String getPlayer4() {
		return player4;
	}

	public void setPlayer4(String player4) {
		this.player4 = player4;
	}

	/**
	 * Create the frame.
	 */

	private void sendGuess(String guess) {
		if(isTurn()){
			if(isRodou()){

				Message message = new Message();
				message.setName(getPlayerName());
				message.setPartida(getPartida());
				message.setText(guess);
				message.setAction(Action.GUESS);
				this.service.send(message);	
			}	else{
				JOptionPane.showMessageDialog(this, "Espere sua vez!");
				
			}
		}else{
			JOptionPane.showMessageDialog(this, "Rode a Roleta!");
		}


	}

	public void receiveNextPlayer(Message message) {
		
		lblBancada1.setEnabled(false);
		lblBancada2.setEnabled(false);
		lblBancada3.setEnabled(false);
		lblBancada4.setEnabled(false);
		btnRodar.setEnabled(false);
		lblponto1.setForeground(Color.BLACK);
		lblponto2.setForeground(Color.BLACK);
		lblponto3.setForeground(Color.BLACK);
		lblponto4.setForeground(Color.BLACK);
		lblponto5.setForeground(Color.BLACK);
		lblponto6.setForeground(Color.BLACK);
		
		if(message.getNextPlayer().equals(getMyNumber())){
			setTurn(true);
			btnRodar.setEnabled(true);
		}else{
			setTurn(false);
		}
		setCurrentPlayer(message.getNextPlayer());
		if(message.getNextPlayer().equals("1")){
			lblBancada1.setEnabled(true);
		}else if(message.getNextPlayer().equals("2")){
			lblBancada2.setEnabled(true);
		}else if(message.getNextPlayer().equals("3")){
			lblBancada3.setEnabled(true);
		}else if(message.getNextPlayer().equals("4")){
			lblBancada4.setEnabled(true);
		}
			
	}

	public void receiveGuess(Message message) {
		if (message.getText().equals(btnA.getText())) {
			btnA.setEnabled(false);
		} else if (message.getText().equals(btnB.getText())) {
			btnB.setEnabled(false);
		} else if (message.getText().equals(btnC.getText())) {
			btnC.setEnabled(false);
		} else if (message.getText().equals(btnD.getText())) {
			btnD.setEnabled(false);
		} else if (message.getText().equals(btnE.getText())) {
			btnE.setEnabled(false);
		} else if (message.getText().equals(btnF.getText())) {
			btnF.setEnabled(false);
		} else if (message.getText().equals(btnG.getText())) {
			btnG.setEnabled(false);
		} else if (message.getText().equals(btnH.getText())) {
			btnH.setEnabled(false);
		} else if (message.getText().equals(btnI.getText())) {
			btnI.setEnabled(false);
		} else if (message.getText().equals(btnJ.getText())) {
			btnJ.setEnabled(false);
		} else if (message.getText().equals(btnL.getText())) {
			btnL.setEnabled(false);
		} else if (message.getText().equals(btnM.getText())) {
			btnM.setEnabled(false);
		} else if (message.getText().equals(btnN.getText())) {
			btnN.setEnabled(false);
		} else if (message.getText().equals(btnO.getText())) {
			btnO.setEnabled(false);
		} else if (message.getText().equals(btnP.getText())) {
			btnP.setEnabled(false);
		} else if (message.getText().equals(btnQ.getText())) {
			btnQ.setEnabled(false);
		} else if (message.getText().equals(btnR.getText())) {
			btnR.setEnabled(false);
		} else if (message.getText().equals(btnS.getText())) {
			btnS.setEnabled(false);
		} else if (message.getText().equals(btnT.getText())) {
			btnT.setEnabled(false);
		} else if (message.getText().equals(btnU.getText())) {
			btnU.setEnabled(false);
		} else if (message.getText().equals(btnV.getText())) {
			btnV.setEnabled(false);
		} else if (message.getText().equals(btnX.getText())) {
			btnX.setEnabled(false);
		} else if (message.getText().equals(btnZ.getText())) {
			btnZ.setEnabled(false);
		} else if (message.getText().equals(btnW.getText())) {
			btnW.setEnabled(false);
		} else if (message.getText().equals(btnK.getText())) {
			btnK.setEnabled(false);
		} else if (message.getText().equals(btnY.getText())) {
			btnY.setEnabled(false);
		}
		System.out.println("PALAVRA: " + getPalavraRodada());
		String palavra = getPalavraRodada();
		StringBuilder stringBuilder = new StringBuilder(this.mascara);
		int numAcertos = 0;

		for (int i = 0; i < this.palavraRodada.length(); i++) {
			String temp = Character.toString(palavra.charAt(i));

			if (temp.equals(message.getText())) {
				numAcertos++;
				stringBuilder.replace(i, i + 1, message.getText());
			}

		}
		
		lblPalavra.setText(stringBuilder.toString());
		setMascara(stringBuilder.toString());
		
		if(isTurn()){
			
			setValor(getValor()*numAcertos);
			if(getMyNumber().equals("1")){
				int placarAtual =  Integer.parseInt(lblPlacar1.getText());
				lblPlacar1.setText(String.valueOf(placarAtual+getValor()));
			}else if(getMyNumber().equals("2")){
				int placarAtual =  Integer.parseInt(lblPlacar2.getText());
				lblPlacar2.setText(String.valueOf(placarAtual+getValor()));
			}else if(getMyNumber().equals("3")){
				int placarAtual =  Integer.parseInt(lblPlacar3.getText());
				lblPlacar3.setText(String.valueOf(placarAtual+getValor()));
			}else if(getMyNumber().equals("4")){
				int placarAtual =  Integer.parseInt(lblPlacar4.getText());
				lblPlacar4.setText(String.valueOf(placarAtual+getValor()));
			}
			
			nextPlayer();
			setTurn(false);
		}else{
			setValor(getValor()*numAcertos);
			if(getCurrentPlayer().equals("1")){
				int placarAtual =  Integer.parseInt(lblPlacar1.getText());
				lblPlacar1.setText(String.valueOf(placarAtual+getValor()));
			}else if(getCurrentPlayer().equals("2")){
				int placarAtual =  Integer.parseInt(lblPlacar2.getText());
				lblPlacar2.setText(String.valueOf(placarAtual+getValor()));
			}else if(getCurrentPlayer().equals("3")){
				int placarAtual =  Integer.parseInt(lblPlacar3.getText());
				lblPlacar3.setText(String.valueOf(placarAtual+getValor()));
			}else if(getCurrentPlayer().equals("4")){
				int placarAtual =  Integer.parseInt(lblPlacar4.getText());
				lblPlacar4.setText(String.valueOf(placarAtual+getValor()));
			}			
			setValor(0);
		}
	}

	public TelaJogo(String ip, int porta) {

		this.service = new ClienteService(ip, porta);
		this.socket = service.connect();
		this.playerName = "";
		this.partida = "";
		this.currentPlayer = "0";
		setTurn(false);

		setResizable(false);
		setMaximizedBounds(new Rectangle(0, 0, 0, 0));
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 1114, 719);
		contentPane = new JPanel();
		contentPane.setBackground(new Color(0, 0, 51));
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);

		JLayeredPane layeredPane = new JLayeredPane();

		JLayeredPane layeredPane_1 = new JLayeredPane();

		bntIniciar = new JButton("Iniciar");
		bntIniciar.setEnabled(false);
		bntIniciar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				nextPlayer();
				askWord();
				bntIniciar.setEnabled(false);
			}
		});
		bntIniciar.setFont(new Font("Microsoft New Tai Lue", Font.PLAIN, 16));

		JButton btnSair = new JButton("Sair");
		btnSair.setFont(new Font("Microsoft New Tai Lue", Font.PLAIN, 16));

		btnRodar = new JButton("Rodar");
		btnRodar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				roda();
			}
		});
		btnRodar.setEnabled(false);
		btnRodar.setFont(new Font("Microsoft New Tai Lue", Font.PLAIN, 16));

		JLabel lbltexto = new JLabel("A palavra \u00E9 :");
		lbltexto.setForeground(Color.LIGHT_GRAY);
		lbltexto.setFont(new Font("Microsoft New Tai Lue", Font.BOLD, 18));

		JLabel lblLogo = new JLabel("");
		lblLogo.setIcon(new ImageIcon("res/img\\Logo - C\u00F3pia.png"));

		lblPalavra = new JLabel("P A L A V R A");
		lblPalavra.setEnabled(false);
		lblPalavra.setForeground(Color.WHITE);
		lblPalavra.setFont(new Font("Tahoma", Font.BOLD, 54));

		lblNewLabel = new JLabel("Dica :  Palavra");
		lblNewLabel.setEnabled(false);
		lblNewLabel.setForeground(Color.LIGHT_GRAY);
		lblNewLabel.setFont(new Font("Microsoft New Tai Lue", Font.BOLD, 18));
		
		lblNewLabel_1 = new JLabel("Chutar Palavra :");
		lblNewLabel_1.setForeground(Color.WHITE);
		lblNewLabel_1.setFont(new Font("Microsoft New Tai Lue", Font.BOLD, 16));
		
		txtPalavra = new JTextField();
		txtPalavra.setText("\r\n");
		txtPalavra.setFont(new Font("Microsoft New Tai Lue", Font.PLAIN, 14));
		txtPalavra.setColumns(10);
		
		JButton btnNewButton = new JButton("Chutar");
		btnNewButton.setFont(new Font("Microsoft New Tai Lue", Font.PLAIN, 12));
		GroupLayout gl_contentPane = new GroupLayout(contentPane);
		gl_contentPane.setHorizontalGroup(
			gl_contentPane.createParallelGroup(Alignment.TRAILING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addContainerGap()
					.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_contentPane.createSequentialGroup()
							.addComponent(layeredPane_1, GroupLayout.PREFERRED_SIZE, 350, GroupLayout.PREFERRED_SIZE)
							.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
								.addGroup(gl_contentPane.createSequentialGroup()
									.addGap(142)
									.addComponent(lblLogo, GroupLayout.PREFERRED_SIZE, 220, GroupLayout.PREFERRED_SIZE))
								.addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING)
									.addGroup(gl_contentPane.createSequentialGroup()
										.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
											.addGroup(gl_contentPane.createSequentialGroup()
												.addGap(112)
												.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
													.addComponent(lblPalavra)
													.addComponent(lbltexto)))
											.addGroup(gl_contentPane.createSequentialGroup()
												.addPreferredGap(ComponentPlacement.RELATED)
												.addComponent(lblNewLabel_1)
												.addPreferredGap(ComponentPlacement.RELATED)
												.addComponent(txtPalavra, GroupLayout.PREFERRED_SIZE, 136, GroupLayout.PREFERRED_SIZE)
												.addPreferredGap(ComponentPlacement.UNRELATED)
												.addComponent(btnNewButton)))
										.addPreferredGap(ComponentPlacement.RELATED, 156, Short.MAX_VALUE)
										.addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING)
											.addComponent(btnSair)
											.addComponent(bntIniciar)))
									.addGroup(Alignment.LEADING, gl_contentPane.createSequentialGroup()
										.addGap(223)
										.addComponent(lblNewLabel))))
							.addGap(47))
						.addGroup(Alignment.TRAILING, gl_contentPane.createSequentialGroup()
							.addComponent(layeredPane, GroupLayout.PREFERRED_SIZE, 1039, GroupLayout.PREFERRED_SIZE)
							.addGap(24))))
				.addGroup(Alignment.LEADING, gl_contentPane.createSequentialGroup()
					.addGap(143)
					.addComponent(btnRodar)
					.addContainerGap(880, Short.MAX_VALUE))
		);
		gl_contentPane.setVerticalGroup(
			gl_contentPane.createParallelGroup(Alignment.TRAILING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_contentPane.createSequentialGroup()
							.addContainerGap()
							.addComponent(layeredPane_1, GroupLayout.PREFERRED_SIZE, 270, GroupLayout.PREFERRED_SIZE))
						.addGroup(gl_contentPane.createSequentialGroup()
							.addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING)
								.addGroup(gl_contentPane.createSequentialGroup()
									.addComponent(lblLogo)
									.addGap(28)
									.addComponent(lbltexto))
								.addComponent(bntIniciar))
							.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
								.addGroup(gl_contentPane.createSequentialGroup()
									.addGap(18)
									.addComponent(lblPalavra)
									.addPreferredGap(ComponentPlacement.UNRELATED)
									.addComponent(lblNewLabel))
								.addGroup(gl_contentPane.createSequentialGroup()
									.addPreferredGap(ComponentPlacement.RELATED)
									.addComponent(btnSair)))))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(btnRodar)
					.addGap(29)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
						.addComponent(txtPalavra, GroupLayout.PREFERRED_SIZE, 21, GroupLayout.PREFERRED_SIZE)
						.addComponent(btnNewButton)
						.addComponent(lblNewLabel_1))
					.addGap(18)
					.addComponent(layeredPane, GroupLayout.PREFERRED_SIZE, 293, GroupLayout.PREFERRED_SIZE))
		);

		lblponto1 = new JLabel("100");
		lblponto1.setFont(new Font("Microsoft New Tai Lue", Font.BOLD, 18));
		lblponto1.setBounds(159, 35, 32, 22);
		layeredPane_1.add(lblponto1);

		lblponto2 = new JLabel("50");
		lblponto2.setFont(new Font("Microsoft New Tai Lue", Font.BOLD, 18));
		lblponto2.setBounds(90, 166, 25, 22);
		layeredPane_1.add(lblponto2);

		lblponto3 = new JLabel("200");
		lblponto3.setFont(new Font("Microsoft New Tai Lue", Font.BOLD, 16));
		lblponto3.setBounds(155, 208, 36, 22);
		layeredPane_1.add(lblponto3);

		lblponto4 = new JLabel("Passou");
		lblponto4.setFont(new Font("Microsoft New Tai Lue", Font.BOLD, 16));
		lblponto4.setBounds(75, 79, 54, 27);
		layeredPane_1.add(lblponto4);

		lblponto5 = new JLabel("Perdeu");
		lblponto5.setFont(new Font("Microsoft New Tai Lue", Font.BOLD, 16));
		lblponto5.setBounds(220, 167, 54, 22);
		layeredPane_1.add(lblponto5);

		lblponto6 = new JLabel("150");
		lblponto6.setFont(new Font("Microsoft New Tai Lue", Font.BOLD, 16));
		lblponto6.setBounds(236, 82, 32, 20);
		layeredPane_1.add(lblponto6);

		JLabel lblRoleta = new JLabel("");
		lblRoleta.setIcon(new ImageIcon("res/img\\Roleta.png"));
		lblRoleta.setBounds(10, 0, 318, 267);
		layeredPane_1.add(lblRoleta);

		lblPlacar1 = new JLabel();
		lblPlacar1.setHorizontalAlignment(SwingConstants.RIGHT);
		lblPlacar1.setBackground(Color.WHITE);
		lblPlacar1.setForeground(Color.BLACK);
		lblPlacar1.setFont(new Font("Microsoft New Tai Lue", Font.BOLD, 20));
		lblPlacar1.setBounds(44, 153, 102, 38);
		layeredPane.add(lblPlacar1);

		lblJogador1 = new JLabel();
		lblJogador1.setHorizontalAlignment(SwingConstants.CENTER);
		lblJogador1.setForeground(Color.BLACK);
		lblJogador1.setFont(new Font("Microsoft New Tai Lue", Font.BOLD, 18));
		lblJogador1.setBounds(44, 231, 102, 20);
		layeredPane.add(lblJogador1);

		lblBancada1 = new JLabel("");
		lblBancada1.setEnabled(false);
		lblBancada1.setIcon(new ImageIcon("res/img\\imgBancada1.png"));
		lblBancada1.setBounds(27, 139, 140, 152);
		layeredPane.add(lblBancada1);

		lblPlacar2 = new JLabel();
		lblPlacar2.setHorizontalAlignment(SwingConstants.RIGHT);
		lblPlacar2.setBounds(307, 154, 102, 37);
		layeredPane.add(lblPlacar2);
		lblPlacar2.setFont(new Font("Microsoft New Tai Lue", Font.BOLD, 20));

		lblJogador2 = new JLabel();
		lblJogador2.setHorizontalAlignment(SwingConstants.CENTER);
		lblJogador2.setForeground(Color.BLACK);
		lblJogador2.setBackground(Color.WHITE);
		lblJogador2.setFont(new Font("Microsoft New Tai Lue", Font.BOLD, 18));
		lblJogador2.setBounds(307, 231, 102, 18);
		layeredPane.add(lblJogador2);

		lblBancada2 = new JLabel("");
		lblBancada2.setEnabled(false);
		lblBancada2.setIcon(new ImageIcon("res/img\\imgBancada2.png"));
		lblBancada2.setBounds(290, 139, 140, 152);
		layeredPane.add(lblBancada2);

		lblPlacar3 = new JLabel();
		lblPlacar3.setHorizontalAlignment(SwingConstants.RIGHT);
		lblPlacar3.setFont(new Font("Microsoft New Tai Lue", Font.BOLD, 20));
		lblPlacar3.setBounds(581, 153, 102, 38);
		layeredPane.add(lblPlacar3);

		lblJogador3 = new JLabel();
		lblJogador3.setHorizontalAlignment(SwingConstants.CENTER);
		lblJogador3.setFont(new Font("Microsoft New Tai Lue", Font.BOLD, 18));
		lblJogador3.setBounds(581, 234, 102, 16);
		layeredPane.add(lblJogador3);

		lblBancada3 = new JLabel("");
		lblBancada3.setEnabled(false);
		lblBancada3.setIcon(new ImageIcon("res/img\\imgBancada3.png"));
		lblBancada3.setBounds(561, 139, 140, 152);
		layeredPane.add(lblBancada3);

		lblPlacar4 = new JLabel();
		lblPlacar4.setHorizontalAlignment(SwingConstants.RIGHT);
		lblPlacar4.setFont(new Font("Microsoft New Tai Lue", Font.BOLD, 20));
		lblPlacar4.setBounds(857, 153, 102, 38);
		layeredPane.add(lblPlacar4);

		lblJogador4 = new JLabel();
		lblJogador4.setHorizontalAlignment(SwingConstants.CENTER);
		lblJogador4.setFont(new Font("Microsoft New Tai Lue", Font.BOLD, 18));
		lblJogador4.setBounds(857, 231, 102, 18);
		layeredPane.add(lblJogador4);

		lblBancada4 = new JLabel("");
		lblBancada4.setEnabled(false);
		lblBancada4.setIcon(new ImageIcon("res/img\\imgBancada4.png"));
		lblBancada4.setBounds(836, 139, 140, 152);
		layeredPane.add(lblBancada4);

		btnA = new JButton("A");
		btnA.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				sendGuess("A");
			}
		});
		btnA.setFont(new Font("Microsoft New Tai Lue", Font.BOLD, 14));
		btnA.setBounds(132, 0, 48, 44);
		layeredPane.add(btnA);

		btnB = new JButton("B");
		btnB.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				sendGuess("B");
			}
		});
		btnB.setFont(new Font("Microsoft New Tai Lue", Font.BOLD, 14));
		btnB.setBounds(190, 0, 48, 44);
		layeredPane.add(btnB);

		btnC = new JButton("C");
		btnC.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				sendGuess("C");
			}
		});
		btnC.setFont(new Font("Microsoft New Tai Lue", Font.BOLD, 14));
		btnC.setBounds(248, 0, 48, 44);
		layeredPane.add(btnC);

		btnD = new JButton("D");
		btnD.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				sendGuess("D");
			}
		});
		btnD.setFont(new Font("Microsoft New Tai Lue", Font.BOLD, 14));
		btnD.setBounds(307, 0, 48, 44);
		layeredPane.add(btnD);

		btnE = new JButton("E");
		btnE.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				sendGuess("E");
			}
		});
		btnE.setFont(new Font("Microsoft New Tai Lue", Font.BOLD, 14));
		btnE.setBounds(365, 0, 48, 44);
		layeredPane.add(btnE);

		btnF = new JButton("F");
		btnF.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				sendGuess("F");
			}
		});
		btnF.setFont(new Font("Microsoft New Tai Lue", Font.BOLD, 14));
		btnF.setBounds(423, 0, 48, 44);
		layeredPane.add(btnF);

		btnG = new JButton("G");
		btnG.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				sendGuess("G");
			}
		});
		btnG.setFont(new Font("Microsoft New Tai Lue", Font.BOLD, 14));
		btnG.setBounds(481, 0, 48, 44);
		layeredPane.add(btnG);

		btnH = new JButton("H");
		btnH.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				sendGuess("H");
			}
		});
		btnH.setFont(new Font("Microsoft New Tai Lue", Font.BOLD, 12));
		btnH.setBounds(539, 0, 48, 44);
		layeredPane.add(btnH);

		btnI = new JButton("I");
		btnI.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				sendGuess("I");
			}
		});
		btnI.setFont(new Font("Microsoft New Tai Lue", Font.BOLD, 14));
		btnI.setBounds(597, 0, 48, 44);
		layeredPane.add(btnI);

		btnJ = new JButton("J");
		btnJ.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				sendGuess("J");
			}
		});
		btnJ.setFont(new Font("Microsoft New Tai Lue", Font.BOLD, 14));
		btnJ.setBounds(655, 0, 48, 44);
		layeredPane.add(btnJ);

		btnK = new JButton("K");
		btnK.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				sendGuess("K");
			}
		});
		btnK.setFont(new Font("Microsoft New Tai Lue", Font.BOLD, 14));
		btnK.setBounds(713, 0, 48, 44);
		layeredPane.add(btnK);

		btnM = new JButton("M");
		btnM.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				sendGuess("M");
			}
		});
		btnM.setFont(new Font("Microsoft New Tai Lue", Font.BOLD, 14));
		btnM.setBounds(829, 0, 48, 44);
		layeredPane.add(btnM);

		btnL = new JButton("L");
		btnL.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				sendGuess("L");
			}
		});
		btnL.setFont(new Font("Microsoft New Tai Lue", Font.BOLD, 14));
		btnL.setBounds(771, 0, 48, 44);
		layeredPane.add(btnL);

		btnN = new JButton("N");
		btnN.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				sendGuess("N");
			}
		});
		btnN.setFont(new Font("Microsoft New Tai Lue", Font.BOLD, 14));
		btnN.setBounds(132, 55, 48, 44);
		layeredPane.add(btnN);

		btnO = new JButton("O");
		btnO.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				sendGuess("O");
			}
		});
		btnO.setFont(new Font("Microsoft New Tai Lue", Font.BOLD, 14));
		btnO.setBounds(190, 55, 48, 44);
		layeredPane.add(btnO);

		btnP = new JButton("P");
		btnP.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				sendGuess("P");
			}
		});
		btnP.setFont(new Font("Microsoft New Tai Lue", Font.BOLD, 14));
		btnP.setBounds(248, 55, 48, 44);
		layeredPane.add(btnP);

		btnQ = new JButton("Q");
		btnQ.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				sendGuess("Q");
			}
		});
		btnQ.setFont(new Font("Microsoft New Tai Lue", Font.BOLD, 14));
		btnQ.setBounds(307, 55, 48, 44);
		layeredPane.add(btnQ);

		btnR = new JButton("R");
		btnR.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				sendGuess("R");
			}
		});
		btnR.setFont(new Font("Microsoft New Tai Lue", Font.BOLD, 14));
		btnR.setBounds(365, 55, 48, 44);
		layeredPane.add(btnR);

		btnS = new JButton("S");
		btnS.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				sendGuess("S");
			}
		});
		btnS.setFont(new Font("Microsoft New Tai Lue", Font.BOLD, 14));
		btnS.setBounds(423, 55, 48, 44);
		layeredPane.add(btnS);

		btnT = new JButton("T");
		btnT.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				sendGuess("T");
			}
		});
		btnT.setFont(new Font("Microsoft New Tai Lue", Font.BOLD, 14));
		btnT.setBounds(481, 55, 48, 44);
		layeredPane.add(btnT);

		btnU = new JButton("U");
		btnU.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				sendGuess("U");
			}
		});
		btnU.setFont(new Font("Microsoft New Tai Lue", Font.BOLD, 14));
		btnU.setBounds(539, 55, 48, 44);
		layeredPane.add(btnU);

		btnW = new JButton("W");
		btnW.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				sendGuess("W");
			}
		});
		btnW.setFont(new Font("Microsoft New Tai Lue", Font.BOLD, 14));
		btnW.setBounds(655, 55, 48, 44);
		layeredPane.add(btnW);

		btnX = new JButton("X");
		btnX.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				sendGuess("X");
			}
		});
		btnX.setFont(new Font("Microsoft New Tai Lue", Font.BOLD, 14));
		btnX.setBounds(713, 55, 48, 44);
		layeredPane.add(btnX);

		btnY = new JButton("Y");
		btnY.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				sendGuess("Y");
			}
		});
		btnY.setFont(new Font("Microsoft New Tai Lue", Font.BOLD, 14));
		btnY.setBounds(771, 55, 48, 44);
		layeredPane.add(btnY);

		btnZ = new JButton("Z");
		btnZ.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				sendGuess("Z");
			}
		});
		btnZ.setFont(new Font("Microsoft New Tai Lue", Font.BOLD, 14));
		btnZ.setBounds(829, 55, 48, 44);
		layeredPane.add(btnZ);

		btnV = new JButton("V");
		btnV.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				sendGuess("V");
			}
		});
		btnV.setFont(new Font("Microsoft New Tai Lue", Font.BOLD, 14));
		btnV.setBounds(597, 55, 48, 44);
		layeredPane.add(btnV);
		// layeredPane.setFocusTraversalPolicy(new FocusTraversalOnArray(new
		// Component[]{lblPlacar1}));
		contentPane.setLayout(gl_contentPane);
	}

	public String getPlayerName() {
		return playerName;
	}

	public void setPlayerName(String playerName) {
		this.playerName = playerName;
	}

	public void refresh(Message message) {
		if(getPlayerName().equals("")){
			setPlayerName(message.getName());
		}

		this.message = message;
		if (message.getMapPoints().size() == 1) {
			setPlayer1((new ArrayList<String>(message.getMapPoints().keySet()))
					.get(0));
			lblJogador1.setText((new ArrayList<String>(message.getMapPoints()
					.keySet())).get(0));
			lblPlacar1.setText(String.valueOf((new ArrayList<Integer>(message
					.getMapPoints().values())).get(0)));
			lblBancada1.setEnabled(true);
			setCreator(true);
			bntIniciar.setEnabled(true);
			setnPlayers("1");
			if (getPlayer1().equals(getPlayerName())){
				
			}
		} else if (message.getMapPoints().size() == 2) {
			setPlayer1((new ArrayList<String>(message.getMapPoints().keySet()))
					.get(0));
			setPlayer2((new ArrayList<String>(message.getMapPoints().keySet()))
					.get(1));
			lblJogador1.setText((new ArrayList<String>(message.getMapPoints()
					.keySet())).get(0));
			lblJogador2.setText((new ArrayList<String>(message.getMapPoints()
					.keySet())).get(1));
			lblPlacar1.setText(String.valueOf((new ArrayList<Integer>(message
					.getMapPoints().values())).get(0)));
			lblPlacar2.setText(String.valueOf((new ArrayList<Integer>(message
					.getMapPoints().values())).get(1)));
			lblBancada1.setEnabled(true);
			lblBancada2.setEnabled(true);
			setnPlayers("2");
		} else if (message.getMapPoints().size() == 3) {
			setPlayer1((new ArrayList<String>(message.getMapPoints().keySet()))
					.get(0));
			setPlayer2((new ArrayList<String>(message.getMapPoints().keySet()))
					.get(1));
			setPlayer3((new ArrayList<String>(message.getMapPoints().keySet()))
					.get(2));
			lblJogador1.setText((new ArrayList<String>(message.getMapPoints()
					.keySet())).get(0));
			lblJogador2.setText((new ArrayList<String>(message.getMapPoints()
					.keySet())).get(1));
			lblJogador3.setText((new ArrayList<String>(message.getMapPoints()
					.keySet())).get(2));
			lblPlacar1.setText(String.valueOf((new ArrayList<Integer>(message
					.getMapPoints().values())).get(0)));
			lblPlacar2.setText(String.valueOf((new ArrayList<Integer>(message
					.getMapPoints().values())).get(1)));
			lblPlacar3.setText(String.valueOf((new ArrayList<Integer>(message
					.getMapPoints().values())).get(2)));
			lblBancada1.setEnabled(true);
			lblBancada2.setEnabled(true);
			lblBancada3.setEnabled(true);
			setnPlayers("3");
		} else if (message.getMapPoints().size() == 4) {
			setPlayer1((new ArrayList<String>(message.getMapPoints().keySet()))
					.get(0));
			setPlayer2((new ArrayList<String>(message.getMapPoints().keySet()))
					.get(1));
			setPlayer3((new ArrayList<String>(message.getMapPoints().keySet()))
					.get(2));
			setPlayer4((new ArrayList<String>(message.getMapPoints().keySet()))
					.get(3));
			lblJogador1.setText((new ArrayList<String>(message.getMapPoints()
					.keySet())).get(0));
			lblJogador2.setText((new ArrayList<String>(message.getMapPoints()
					.keySet())).get(1));
			lblJogador3.setText((new ArrayList<String>(message.getMapPoints()
					.keySet())).get(2));
			lblJogador4.setText((new ArrayList<String>(message.getMapPoints()
					.keySet())).get(3));
			lblPlacar1.setText(String.valueOf((new ArrayList<Integer>(message
					.getMapPoints().values())).get(0)));
			lblPlacar2.setText(String.valueOf((new ArrayList<Integer>(message
					.getMapPoints().values())).get(1)));
			lblPlacar3.setText(String.valueOf((new ArrayList<Integer>(message
					.getMapPoints().values())).get(2)));
			lblPlacar4.setText(String.valueOf((new ArrayList<Integer>(message
					.getMapPoints().values())).get(3)));
			lblBancada1.setEnabled(true);
			lblBancada2.setEnabled(true);
			lblBancada3.setEnabled(true);
			lblBancada4.setEnabled(true);
			setnPlayers("4");
			
		}
		if (getPlayer1().equals(getPlayerName())){
			setMyNumber("1");
		} else if (getPlayer2().equals(getPlayerName())){
			setMyNumber("2");			
		}else if (getPlayer3().equals(getPlayerName())){
			setMyNumber("3");		
		}else if (getPlayer4().equals(getPlayerName())){
			setMyNumber("4");			
		}
		System.out.println("MY NUMB: "+getMyNumber());
		setMatchCreator(message.getMatchCreator());
	}
}
